from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.auth.models import User

from .models import (
    AggregatedFinancialReport,
    Company,
    CompanyMember,
    Department,
    FinanceCategory,
    FinanceRecord,
    FinancialAnalysis,
    SmartFinancialReport,
    UserProfile,
)


@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    list_display = ['name', 'legal_person', 'industry', 'registration_number', 'registration_date']
    list_filter = ['industry', 'registration_date', 'created_at']
    search_fields = ['name', 'legal_person', 'registration_number']
    ordering = ['name']


@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    list_display = ['name', 'company', 'manager', 'created_at']
    list_filter = ['company', 'created_at']
    search_fields = ['name', 'manager', 'company__name']
    ordering = ['company', 'name']


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'company', 'phone', 'role', 'department', 'hire_date']
    list_filter = ['role', 'department', 'company']
    search_fields = ['user__username', 'user__first_name', 'user__last_name', 'phone', 'company__name']
    ordering = ['company', 'user__username']


@admin.register(FinanceCategory)
class FinanceCategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'type', 'parent', 'created_at']
    list_filter = ['type', 'parent', 'created_at']
    search_fields = ['name', 'description']
    ordering = ['type', 'name']
    list_editable = ['type']


@admin.register(FinanceRecord)
class FinanceRecordAdmin(admin.ModelAdmin):
    list_display = ['date', 'company', 'department', 'category', 'amount', 'user_profile', 'source', 'created_at']
    list_filter = ['date', 'company', 'department', 'category__type', 'source', 'created_at']
    search_fields = ['description', 'category__name', 'department__name', 'company__name']
    list_editable = ['amount']
    date_hierarchy = 'date'
    ordering = ['-date', '-created_at']


@admin.register(AggregatedFinancialReport)
class AggregatedFinancialReportAdmin(admin.ModelAdmin):
    list_display = ['aggregation_type', 'period', 'department', 'category', 'total_income', 'total_expense', 'net_profit', 'transaction_count', 'generated_by', 'generated_at']
    list_filter = ['aggregation_type', 'period', 'department', 'category', 'generated_at']
    search_fields = ['period']
    date_hierarchy = 'generated_at'
    ordering = ['-generated_at']
    autocomplete_fields = ['generated_by']


@admin.register(CompanyMember)
class CompanyMemberAdmin(admin.ModelAdmin):
    list_display = ['company', 'user_profile', 'role', 'equity_ratio', 'join_date', 'is_active']
    list_filter = ['role', 'is_active', 'join_date', 'company']
    search_fields = ['company__name', 'user_profile__user__username']
    list_editable = ['role', 'is_active']
    ordering = ['company', '-join_date']


@admin.register(SmartFinancialReport)
class SmartFinancialReportAdmin(admin.ModelAdmin):
    list_display = ['company', 'report_type', 'period', 'revenue', 'expenses', 'profit', 'created_at']
    list_filter = ['report_type', 'created_at', 'company']
    search_fields = ['company__name', 'period']
    ordering = ['-created_at']


@admin.register(FinancialAnalysis)
class FinancialAnalysisAdmin(admin.ModelAdmin):
    list_display = ['report', 'risk_level', 'created_at']
    list_filter = ['risk_level', 'created_at']
    search_fields = ['key_insights', 'ai_suggestions', 'report__period']
    date_hierarchy = 'created_at'
    ordering = ['-created_at']
    autocomplete_fields = ['report']


class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = '用户档案'


class CustomUserAdmin(DjangoUserAdmin):
    inlines = (UserProfileInline,)
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'get_phone', 'get_role', 'get_department', 'get_company')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'date_joined', 'profile__role', 'profile__department', 'profile__company')
    search_fields = ('username', 'first_name', 'last_name', 'email', 'profile__phone', 'profile__company__name')

    def get_phone(self, obj):
        return obj.profile.phone if hasattr(obj, 'profile') else '-'

    get_phone.short_description = '手机号'

    def get_role(self, obj):
        return obj.profile.get_role_display() if hasattr(obj, 'profile') else '-'

    get_role.short_description = '角色'

    def get_department(self, obj):
        return obj.profile.department if hasattr(obj, 'profile') else '-'

    get_department.short_description = '部门'

    def get_company(self, obj):
        return obj.profile.company if hasattr(obj, 'profile') else '-'

    get_company.short_description = '公司'


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)